package com.whq.thrift.api.web.admin.controller.bs;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.consumelog.ConsumeLog;
import com.whq.thrift.api.web.admin.service.consumelog.ConsumeLogService;
import com.whq.thrift.api.web.admin.service.verify.MethodInfoService;
import com.whq.thrift.api.web.admin.util.DateFormatUtil;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class ConsumeLogController {
	
	private static final int DEFAULT_PAGE_SIZE = 25;
	
	@Autowired
	ConsumeLogService consumeLogService;
	@Autowired
	MethodInfoService methodInfoService;
	
	@RequestMapping(value="bs/consumelog/list")
	public String list(
	        @RequestParam(value="time", required=false) String time,
	        @RequestParam(value="start_time", required=false) String start_time,
	        @RequestParam(value="end_time", required=false) String end_time,
	        @RequestParam(value="api", required=false) String api,
	        @RequestParam(value="code", required=false) String code,
	        @RequestParam(value="flow_id", required=false) String flow_id,
	        @RequestParam(value="result", required=false) Integer result,
	        @RequestParam(value="dir", required=false) Integer dir,
	        @RequestParam(value="currentPage", required=true, defaultValue="1") Integer currentPage,
	        @RequestParam(value="pageSize", required=false) Integer pageSize,
			Model model) {
		ConsumeLog consumeLog = new ConsumeLog();
		PageData<ConsumeLog> pageData = new PageData<ConsumeLog>();
	    pageData.setCurrentPage(currentPage);
	    if ( (pageSize == null) || (pageSize <= 0) ){
	    	pageSize = DEFAULT_PAGE_SIZE;
	    }
	    pageData.setPageSize(pageSize);
		
	 // 查询参数
	    Map<String, Object> whereParams = new HashMap<String, Object>(0);
	    if (time != null && StringUtil.isNotBlank(time)) {
	    	whereParams.put("time", DateFormatUtil.dateToString(DateFormatUtil.formatToDate(time)));
	    	consumeLog.setTime(DateFormatUtil.formatToDate(time));
	    }
	    
	    int tableSeq = consumeLog.getTableSeq();
	    whereParams.put("tableSeq", tableSeq);
	    
	    if (StringUtil.isNotBlank(start_time)) {
	    	whereParams.put("start_time", start_time);
	    } else {
	    	whereParams.put("start_time", "00:00:00");
	    }
	    
	    if (StringUtil.isNotBlank(end_time)) {
	    	whereParams.put("end_time", end_time);
	    } else {
	    	whereParams.put("end_time", "23:59:59");
	    }
	    
	    if (StringUtil.isNotBlank(api)) {
	    	whereParams.put("api", api);
	    }
	    
	    if (StringUtil.isNotBlank(code)) {
	    	whereParams.put("code", code);
	    }
	    
	    if (StringUtil.isNotBlank(flow_id)) {
	    	whereParams.put("flow_id", flow_id);
	    }
	    
	    if(dir!=null){
	    	whereParams.put("dir", dir);
	    }
	    
	    if (result != null) {
	    	whereParams.put("result", result);
	    }
	    
	    int totalCount = consumeLogService.findRecordCount(whereParams);
	    pageData.setTotalCount(totalCount);
	    
	    int beginIndex = (currentPage - 1) * pageSize;
	    
	    List<ConsumeLog> resultList = consumeLogService.findRecordsByMap(whereParams, beginIndex, pageSize);
	    for(ConsumeLog log : resultList) {
	    	handleCodeAndSeqName(log);
	    }
	    pageData.setResultList(resultList);
	    
	    model.addAttribute("time", time);
	    model.addAttribute("start_time", start_time);
	    model.addAttribute("end_time", end_time);
	    model.addAttribute("api", api);
	    model.addAttribute("code", code);
	    model.addAttribute("flow_id", flow_id);
	    model.addAttribute("result",result);
	    model.addAttribute("dir",dir);
		model.addAttribute("pageData", pageData);
		return "bs/consumelog/list";
	}
	
	@RequestMapping(value="bs/consumelog/show")
	public String show(@RequestParam(value="id", required=true) String id,
					   @RequestParam(value="time", required=true) String time,
			Model model){ 
		if (id == null) {
			return "error";
		}
		ConsumeLog record = this.consumeLogService.findRecordById(id, time);
        model.addAttribute("record", record);
        if (record == null) {
			return "error";
		}
        
	    handleCodeAndSeqName(record);
        
        return "bs/consumelog/show";  
    }
	
	private void handleCodeAndSeqName(ConsumeLog log) {
		if (log == null) {
			return;
		}
		
		List<String> seqList = StringUtil.split2list(log.getSeq(), "\\|");
		if (seqList != null) {
			StringBuffer sb = new StringBuffer();
			for (Iterator<String> iterator = seqList.iterator(); iterator.hasNext();) {
				String oneCode = (String) iterator.next();
				sb.append(oneCode + "<br>");
			}
			log.setSeq(sb.toString());
		}
		
	}

}
